package com.leador.location;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import com.leador.api.navi.LeadorNavi;
import com.leador.api.navi.LeadorNaviListener;
import com.leador.api.navi.model.LeadorLaneInfo;
import com.leador.api.navi.model.LeadorNaviCross;
import com.leador.api.navi.model.LeadorNaviLocation;
import com.leador.api.navi.model.LeadorNaviTrafficFacilityInfo;
import com.leador.api.navi.model.LeadorServiceFacilityInfo;
import com.leador.api.navi.model.NaviInfo;
import com.leador.http.HttpManager;
import com.leador.http.ResultParse;
import com.leador.listener.OnEntityListener;
import com.leador.manger.LBSTraceClient;
import com.leador.map.api.location.MapLocation;
import com.leador.module.TraceLocation;
import com.leador.module.response.Result;

import java.io.File;

/**
 * Created by lhy on 2016/9/30.
 */
public class GraspCacth implements LeadorNaviListener{
    private Context mcontext;
    private static GraspCacth INSTANCE = null;
    private LeadorNavi leadorNavi;
    private GraspCacth(Context mcontext){
        String path =   Environment.getExternalStorageDirectory()
                .getAbsolutePath() + File.separator + "Android";
        leadorNavi =  LeadorNavi.getInstance(mcontext);
        leadorNavi.setLocalDataPath(path);
        leadorNavi.addLeadorNaviListener(new LeadorNaviListener() {
            @Override
            public void onInitNaviFailure() {

            }

            @Override
            public void onInitNaviSuccess() {

            }

            @Override
            public void onStartNavi(int i) {

            }

            @Override
            public void onTrafficStatusUpdate() {

            }

            @Override
            public void onLocationChange(LeadorNaviLocation leadorNaviLocation) {
                Log.e("最终的坐标",leadorNaviLocation.getCoord().getLongitude()+","+leadorNaviLocation.getCoord().getLatitude());
            }

            @Override
            public void onGetNavigationText(int i, String s) {

            }

            @Override
            public void onEndEmulatorNavi() {

            }

            @Override
            public void onArriveDestination() {

            }

            @Override
            public void onCalculateRouteSuccess() {

            }

            @Override
            public void onCalculateRouteFailure(int i) {

            }

            @Override
            public void onReCalculateRouteForYaw() {

            }

            @Override
            public void onReCalculateRouteForTrafficJam() {

            }

            @Override
            public void onArrivedWayPoint(int i) {

            }

            @Override
            public void onGpsOpenStatus(boolean b) {

            }

            @Override
            public void onNaviInfoUpdate(NaviInfo naviInfo) {

            }

            @Override
            public void onUpdateTrafficFacility(LeadorNaviTrafficFacilityInfo[] leadorNaviTrafficFacilityInfos) {

            }

            @Override
            public void showCross(LeadorNaviCross leadorNaviCross) {

            }

            @Override
            public void hideCross() {

            }

            @Override
            public void showLaneInfo(LeadorLaneInfo[] leadorLaneInfos, byte[] bytes, byte[] bytes1) {

            }

            @Override
            public void hideLaneInfo() {

            }

            @Override
            public void onCalculateMultipleRoutesSuccess(int[] ints) {

            }

            @Override
            public void updateServiceFacility(LeadorServiceFacilityInfo[] leadorServiceFacilityInfos) {

            }
        });
        this.mcontext = mcontext;
    }
    public static GraspCacth getInstance(Context mcontext){
       if(INSTANCE == null){
           synchronized (GraspCacth.class){
               if(INSTANCE == null){
                   INSTANCE = new GraspCacth(mcontext);
               }
           }
       }
        return INSTANCE;
    }

    /**
     * 获取抓路后的数据
     * @param mapLocation
     */
    public void getCachData(MapLocation mapLocation){
        if(mapLocation == null){
            String message = "{\"status\": \"10011\", \"message\": \"没有可用的坐标\"}";
            //没有可用的坐标
            Result result = ResultParse.resultParse(message);
            LBSTraceClient.onEntityListener.onRequestFailedCallback(result);
            return;
        }
        leadorNavi.setGPSInfo(2,mapLocation);
    }

    @Override
    public void onInitNaviFailure() {

    }

    @Override
    public void onInitNaviSuccess() {

    }

    @Override
    public void onStartNavi(int i) {

    }

    @Override
    public void onTrafficStatusUpdate() {

    }

    @Override
    public void onLocationChange(LeadorNaviLocation leadorNaviLocation) {
        Log.e("最终的坐标",leadorNaviLocation.toString());
//        TraceLocation location = null;
//        try {
////            location = HttpManager.queryRealtimeLoc(leadorNaviLocation);
//            if (location.getLatitude() != 0 && location.getLongitude() != 0) {
//                LBSTraceClient.onEntityListener.onReceiveLocation(location);
//            } else {
//                String message = "{\"status\": \"10011\", \"message\": \"没有可用的坐标\"}";
//                //没有可用的坐标
//                Result result = ResultParse.resultParse(message);
//                LBSTraceClient.onEntityListener.onRequestFailedCallback(result);
//            }
//        }  catch (Exception localException) {
//            Log.e("localException error",localException.getMessage());
//            if ( LBSTraceClient.onEntityListener != null) {
//                String message = "{\"status\": \"10011\", \"message\": \"实时定位失败\"}";
//                Result result = ResultParse.resultParse(message);
//                LBSTraceClient.onEntityListener.onRequestFailedCallback(result);
//            }
//        }
    }

    @Override
    public void onGetNavigationText(int i, String s) {

    }

    @Override
    public void onEndEmulatorNavi() {

    }

    @Override
    public void onArriveDestination() {

    }

    @Override
    public void onCalculateRouteSuccess() {

    }

    @Override
    public void onCalculateRouteFailure(int i) {

    }

    @Override
    public void onReCalculateRouteForYaw() {

    }

    @Override
    public void onReCalculateRouteForTrafficJam() {

    }

    @Override
    public void onArrivedWayPoint(int i) {

    }

    @Override
    public void onGpsOpenStatus(boolean b) {

    }

    @Override
    public void onNaviInfoUpdate(NaviInfo naviInfo) {

    }

    @Override
    public void onUpdateTrafficFacility(LeadorNaviTrafficFacilityInfo[] leadorNaviTrafficFacilityInfos) {

    }

    @Override
    public void showCross(LeadorNaviCross leadorNaviCross) {

    }

    @Override
    public void hideCross() {

    }

    @Override
    public void showLaneInfo(LeadorLaneInfo[] leadorLaneInfos, byte[] bytes, byte[] bytes1) {

    }

    @Override
    public void hideLaneInfo() {

    }

    @Override
    public void onCalculateMultipleRoutesSuccess(int[] ints) {

    }

    @Override
    public void updateServiceFacility(LeadorServiceFacilityInfo[] leadorServiceFacilityInfos) {

    }
}
